import Mock from "mockjs";

// get请求从config.url获取参数，post从config.body中获取参数
function param2Obj(url) {
  const search = url.split("?")[1];
  if (!search) {
    return {};
  }
  return JSON.parse(
    '{"' +
      decodeURIComponent(search)
        .replace(/"/g, '\\"')
        .replace(/&/g, '","')
        .replace(/=/g, '":"') +
      '"}'
  );
}

// let List = [];
// const count = 200;

// for (let i = 0; i < count; i++) {
//   List.push(
//     Mock.mock({
//       id: Mock.Random.guid(),
//       name: Mock.Random.cname(),
//       addr: Mock.mock("@county(true)"),
//       "age|18-60": 1,
//       birth: Mock.Random.date(),
//       sex: Mock.Random.integer(0, 1),
//     })
//   );
// }
let idlist = [];
const count2 = 20;
for (let i = 0; i < count2; i++) {
  idlist.push(
    Mock.mock({
      id: "ns=2;s=Demo.Dynamic.Scalar.Int" + Mock.Random.integer(0, 100),
      numb: Mock.Random.increment(1),
      rw: "rw",
    })
  );
}
const id0 = {
  id: "ns=2;s=Demo.Dynamic.Scalar.Int16",
  numb: "21",
  rw: "r",
};
const id1 = {
  id: "ns=2;s=Demo.Dynamic.Scalar.Int32",
  numb: "22",
  rw: "rw",
};
const id2 = {
  id: "ns=3;s=Furnace_9.Temperature",
  numb: "23",
  rw: "r",
};
const id3 = {
  id: "ns=2;s=Demo.Dynamic.Scalar.Int32",
  numb: "24",
  rw: "rw",
};
idlist.push(id0);
idlist.push(id1);
idlist.push(id2);
idlist.push(id3);

export default {
  /**
   * 获取列表
   * 要带参数 name, page, limt; name可以不填, page,limit有默认值。
   */
  getUserList: (config) => {
    console.log(config.url);
    const { id, page, limit = 10 } = param2Obj(config.url);
    const mockList = idlist.filter((user) => {
      if (id && user.id.indexOf(id) === -1) return false;
      return true;
    });
    const pageList = mockList.filter(
      (item, index) => index < limit * page && index >= limit * (page - 1)
    );
    return {
      code: 200,
      data: {
        // list: idlist,
        // count: idlist.length,
        idList: pageList,
        count: mockList.length,
      },
    };
  },
  /**
   * 增加用户
   */
  createUser: (config) => {
    const { id, rw } = JSON.parse(config.body);
    console.log(JSON.parse(config.body));
    console.log(config.url);
    idlist.unshift({
      id: "ns=2;s=Demo.Dynamic.Scalar.Int" + id,
      numb: Mock.Random.increment(1),
      rw: rw,
    });
    return {
      code: 200,
      data: {},
      message: "添加成功",
    };
  },
  /**
   * 删除用户
   */
  deleteUser: (config) => {
    const { id } = JSON.parse(config.body);
    console.log("id=" + id);
    if (id === "null") {
      return {
        code: -999,
        message: "参数不正确",
      };
    } else {
      idlist = idlist.filter((u) => u.id !== id);
      return {
        code: 200,
        message: "删除成功",
      };
    }
  },

  /**
   * 修改用户
   */
  updateUser: (config) => {
    const { id, value, num } = JSON.parse(config.body);
    console.log(value);
    idlist.some((u) => {
      if (u.id === id) {
        u.id = id;
        u.num = num;
        return true;
      }
    });
    return {
      code: 200,
      data: {},
      message: "编辑成功",
    };
  },
};
